﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;

namespace SMSROUTER.Business
{
    public partial class SmsMTType
    {
        private static object _lock = new object();
        private static List<SmsMTType> _instance;
        public static List<SmsMTType> GetInstance()
        {
            if (_instance == null)
            {
                lock (_lock)
                {
                    using (var context = new DBDataContext(AppSetting.ConnectionString))
                    {
                        _instance = context.SmsMTTypes.ToList();
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.SmsMTTypes.ToList();
                }
            }
        }

        #region Static method
        public static List<SmsMTType> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string mtTypeCode, string mtTypeName)
        {
            var resutl = context.SmsMTTypes.AsQueryable();
            if (!string.IsNullOrWhiteSpace(mtTypeCode))
            {
                resutl = resutl.Where(p => p.MTTypeCode.Contains(mtTypeCode));
            }
            if (!string.IsNullOrWhiteSpace(mtTypeName))
            {
                resutl = resutl.Where(p => p.MTTypeName.Contains(mtTypeName));
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }
        #endregion
    }
}
